package cn.biplam.front.component.data.common;

import cn.biplam.common.client.Paging;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.ScriptSelectInput;
import cn.biplam.front.htmlscript.script.SizeScript;
import cn.biplam.front.template.base.ButtonImageHorizon;
import cn.biplam.front.template.base.Input;
import cn.biplam.front.template.base.Text;

/**
 * 分页组件
 * <ul>
 * 		PagingComp pc = new PagingComp(cId,aId,cId);
 *		pc.setWidth("500");
 *		testPage.put(pc.get(mUser, sHomeUrl, mPaging));
 * </ul>
 * @author WangSong 2009-6-29下午03:13:51
 * @version 1.0
 */
public class PagingComp {

	/** 基础id */
	public static final String baseID = "paging";
	/** 唯一标示ID*/
	private String ID;
	/** 行为化id */
	private String actionID;
	/** 目标容器ID */
	private String targetID;
	/** 宽度*/
	private int width=-1;

	/** 页面需要的参数*/
	private String param;
	/** 附加参数容器ID */
	private String addParamsCId;

	/** 跳转到图片路径 */
	private static final String Page_Back_Ground = "/images/page_comp_bg.gif";
	
	/** 最后一页图片路径 */
	private static final String Last_Page_Pic_Path="/images/page-last.gif";
	/** 最后一页图片无效路径 */
	private static final String Last_Page_Invalid_Pic_Path="/images/page-last-disabled.gif";

	/** 第一页图片路径 */
	private static final String First_Page_Pic_Path="/images/page-first.gif";
	/** 第一页图片无效路径 */
	private static final String First_Page_Invalid_Pic_Path="/images/page-first-disabled.gif";

	/** 上一页图片路径 */
	private static final String Pre_Page_Pic_Path="/images/page-prev.gif";
	/** 上一页无效图片路径 */
	private static final String Pre_Page_Invalid_Pic_Path="/images/page-prev-disabled.gif";

	/** 下一页图片路径 */
	private static final String Next_Page_Pic_Path="/images/page-next.gif";
	/** 下一页图片无效路径 */
	private static final String Next_Page_Invalid_Pic_Path="/images/page-next-disabled.gif";

	/** 跳转到图片路径 */
	private static final String Go_To_Page_Pic_Path = "/images/page_go.gif";

	/** 跳转到页数输入框ID */
	private static final String Go_To_Page_Num_ContainerId = ".goToNumb";

	/**
	 * 分页按钮操作类型 
	 */
	public enum Page_Opt_Type{
		/** 上一页 */
		PrePage,
		/** 下一页 */
		NextPage;
	}

	/**
	 * 构造函数
	 * @param submitCid  提交容器ID唯一标识
	 * @param actionID 	 行为id
	 * @param targetId   目标容器ID
	 */
	public PagingComp(String submitCid,String actionID,String targetId){
		this.ID = baseID + submitCid;
		this.actionID = actionID;
		this.targetID = targetId;
	}

	/**
	 *  设置附加参数容器ID
	 * @param addParamsCId 附加参数容器ID
	 */
	public void setAddParamsCId(String addParamsCId){
		this.addParamsCId = addParamsCId;
	}

	/**
	 * 获取分页控件高度
	 * @return 组件高度
	 */
	public int getHeight(){
		return 22;
	}

	/**
	 * 设置宽度
	 * @param width
	 */
	public void setWidth(String width){
		this.width = TOOLS.stringToInt(width);
	}
	
	/**
	 * 设置宽度
	 * @param width
	 */
	public void setWidth(int width){
		this.width = width;
	}
	
	/**
	 * 取分页页面<BR>
	 * 如果未设置分页组件宽度,分页组件的宽度等于他的父容器的宽度<BR>
	 * @param mUser    用户信息
	 * @param sHomeUrl 系统路径
	 * @param mPaging  分页信息
	 * @param param    页面需要的参数(rowid)
	 * @return HtmlWindow
	 */
	public HtmlWindow get(UserInfo mUser,String sHomeUrl,Paging mPaging,String param) throws ErrorMessage{
		this.param = param;
		HtmlWindow pagingHtml = new HtmlDiv(this.ID);
		if(this.width>0){
			pagingHtml.setStyleWidth(this.width);
		}else{
			pagingHtml.bind(SizeScript.setSizeEqualParent(this.ID));//设置分页对象的宽度等于父容器的宽度
		}
		//第一页
		this.getFirstPagePic(pagingHtml, mPaging, sHomeUrl);
		//上一页
		this.getPrePagePic(pagingHtml,mPaging, sHomeUrl);
		//跳转到
		this.goToPagePic(pagingHtml, mPaging, sHomeUrl);
		//页数信息
		this.getPageInfo(pagingHtml, mUser,mPaging);
		//下一页
		this.getNextPagePic(pagingHtml,mPaging, sHomeUrl);
		//最后一页
		this.getLastPagePic(pagingHtml, mPaging, sHomeUrl);
		/**当前页数参数*/
		pagingHtml.put(new HtmlValue("h,"+this.ID+"pageNum","h,"+this.ID+"pageNum",
				TOOLS.intToString(mPaging.getCurrentPageNum())));
		pagingHtml.put(new HtmlValue("h,"+this.ID+"optIdPage","h,"+this.ID+"optIdPage",
				mPaging.getOptId()));
		
		pagingHtml.setBackGroundImage(sHomeUrl+Page_Back_Ground);
		pagingHtml.setStyleHeight("20");
		return pagingHtml;
	}

	/**
	 * 跳转到按钮
	 */
	private void goToPagePic(HtmlWindow paging,Paging mPaging,
			String sHomeUrl) throws ErrorMessage{
		HtmlDiv div = new HtmlDiv();
		String script = getGotoScript(mPaging);

		String path = Go_To_Page_Pic_Path;
		//创建跳转到页数输入框
		Input input = new Input(
				null,
				Go_To_Page_Num_ContainerId,//输入框ID
				mPaging.getCurrentPageNum()+"", //输入框默认值为当前页数
				3,                              //显示的输入页数最多4位数字
				10                              //输入页数最大为十位数字
		);
		div.put(input.get());
		HtmlWindow pageNumb = input.get();
		pageNumb.bind(Window_Event.OnKeyDown, ScriptSelectInput.inputEnterKeyDown(script));//按回车按键时跳转
		//创建跳转到按钮
		HtmlWindow pic = this.getBasePic(path,sHomeUrl,
				"跳转到",
				script	//点击跳转按钮事件
				);
		div.put(pic);
		div.setFloat(Float_Type.Left);
		div.setMarginLeft("5");
		div.setMarginRight("5");

		paging.put(div);
	}

	/**
	 *   绑定跳转到按钮事件<BR>
		 检查输入框是否数字,不是数字什么都不做<BR>
		 检查输入框的输入数字是否超过记录集页数的最大值,如果超过设置页数为最后一页<BR>
	     输入数字是否小于1页,如果小于设置页数为第一页<BR>
	 * @return 跳转到脚本
	 */
	private String getGotoScript(Paging mPaging){
		StringBuilder sp = new StringBuilder();
		sp.append("tbGoto('");
		sp.append(Go_To_Page_Num_ContainerId+"',"+//输入框ID
				mPaging.getTotal()+",'"+//总页数
				"h,"+this.ID+"pageNum"+"',"+//页数隐藏容器ID
				mPaging.getCurrentPageNum()+",'"+//当前页数
				this.ID+"','"+
				this.actionID+"','"+
				this.param+"','"+
				this.targetID+"'");
		if(this.addParamsCId!=null){
			sp.append(",'");
			sp.append(this.addParamsCId);
			sp.append("'");
		}
		sp.append(")");
		return sp.toString();
	}

	/**
	 * 最后一页按钮
	 */
	private void getLastPagePic(HtmlWindow paging,Paging mPaging,
			String sHomeUrl) throws ErrorMessage{
		String path = null;
		String bindScript = null;
		if(mPaging.isLastPage()){
			path = Last_Page_Invalid_Pic_Path;
		}else{
			path = Last_Page_Pic_Path;
			bindScript = _getBindScript(mPaging.getTotal());//如果当前页不是最后一页绑定跳转到最后一页的动作
		}
		//获取图片按钮
		HtmlWindow pic = this.getBasePic(path,sHomeUrl,
				"最后一页",bindScript);
		paging.put(pic);
	}

	/**
	 *  第一页按钮
	 */
	private void getFirstPagePic(HtmlWindow paging,Paging mPaging,
			String sHomeUrl) throws ErrorMessage{
		String path = null;
		String bindScript = null;
		if(mPaging.isFirstPage()){
			path = First_Page_Invalid_Pic_Path;
		}else{
			path = First_Page_Pic_Path;
			bindScript = _getBindScript(1);//如果当前页不是第一页绑定跳转到第一页的动作
		}
		HtmlWindow pic = this.getBasePic(path,sHomeUrl,
				"第一页",bindScript);
		paging.put(pic);
	}

	/**
	 * 上一页按钮
	 */
	private void getPrePagePic(HtmlWindow paging,Paging mPaging,
			String sHomeUrl) throws ErrorMessage{
		String path = null;
		String bindScript = null;
		if(mPaging.isFirstPage()){
			path = Pre_Page_Invalid_Pic_Path;
		}else{
			path = Pre_Page_Pic_Path;
			bindScript = this._getBindScript(mPaging.getCurrentPageNum()-1);
		}
		HtmlWindow pic = this.getBasePic(path,sHomeUrl,
				"上一页"
				,bindScript//跳转脚本
				);
		paging.put(pic);
	}

	/**
	 * 下一页按钮
	 */
	private void getNextPagePic(HtmlWindow paging,Paging mPaging,
			String sHomeUrl) throws ErrorMessage{
		String path = null;
		String bindScript = null;//跳转脚本
		if(mPaging.isLastPage()){
			path = Next_Page_Invalid_Pic_Path;
		}else{
			path = Next_Page_Pic_Path;
			bindScript = this._getBindScript(mPaging.getCurrentPageNum()+1);
		}
		HtmlWindow pic = this.getBasePic(path,sHomeUrl,
				"下一页",
				bindScript);
		paging.put(pic);
	}

	/**
	 * 取基本图片
	 * @param path
	 * @return	HtmlWindow
	 * @throws ErrorMessage
	 */
	private HtmlWindow getBasePic(String path,String sHomeUrl,
			String sTitle,
			String bindScript) throws ErrorMessage{
		String imageUrl = sHomeUrl+path;
		ButtonImageHorizon button = new ButtonImageHorizon(null,imageUrl,sTitle);
		button.setOnClickScript(bindScript);
		HtmlWindow image =  button.get();
		image.setMarginTop("2");
		return image;
	}

	/**
	 *  获取当前数据集页数信息
	 */
	private void getPageInfo(HtmlWindow paging,UserInfo mUser,Paging mPaging) throws ErrorMessage{

		/**分页信息*/
		StringBuilder sb = new StringBuilder();
		sb.append("第"+mPaging.getCurrentPageNum()+"页 ");
		sb.append("共"+mPaging.getTotal()+"页 ");
		sb.append(mPaging.getTotalLine()+"行");
		Text text = new Text(sb.toString());
		HtmlWindow ht = text.get();
		ht.setMarginLeft("10");
		ht.setMarginRight("10");
		ht.setMarginTop("4");
		paging.put(ht);
	}

	/**
	 * @return 跳转页对应宽度
	 */
	private String _getBindScript(int pageNumb){
		StringBuilder sp = new StringBuilder();
		sp.append("$('"+"h,"+this.ID+"pageNum"+"').value=parseInt("+pageNumb+");");
		sp.append(AjxScript.getM(this.ID,this.actionID, param, this.targetID,this.addParamsCId));
		return sp.toString();
	}
}
